package com.wbdgj.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.internal.LinkedTreeMap;
import com.wbdgj.R;
import com.wbdgj.http.HttpAdapter;

import java.text.DecimalFormat;
import java.util.List;

/**
 * ListAdapter
 * Created by XiaoWei on 2015-11-14.
 */
public class ChoiceBranchOutAdapter extends BaseListAdapter<LinkedTreeMap> {
    private int[] imgId = {R.mipmap.md_001, R.mipmap.md_002, R.mipmap.md_003, R.mipmap.md_004, R.mipmap.md_005, R.mipmap.md_006, R.mipmap.md_007, R.mipmap.md_008};
    public ChoiceBranchOutAdapter(Context context, List<LinkedTreeMap> objects) {
        super(context, objects);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            if (getItemViewType(position) == 0) {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.layout_has_no_data, parent, false);
                holder.noDataRootLayout = (LinearLayout) convertView.findViewById(R.id.root_layout);
            } else {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.ui_choice_branch_out_item, parent, false);
                holder.branchName = (TextView) convertView.findViewById(R.id.branchName);
                holder.money = (TextView) convertView.findViewById(R.id.money);
                holder.status = (TextView) convertView.findViewById(R.id.status);
                holder.mdName = (TextView) convertView.findViewById(R.id.mdName);
                holder.union_type = (TextView) convertView.findViewById(R.id.union_type);
                holder.image = (ImageView) convertView.findViewById(R.id.image);
                holder.mdLay = (LinearLayout) convertView.findViewById(R.id.mdLay);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (hasNoData) {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(getScreenWidth(),getScreenHeight()*2/3);
            holder.noDataRootLayout.setLayoutParams(lp);
        } else {
            final DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
            holder.branchName.setText(mDataList.get(position).get("branch_name")+"");
            holder.mdName.setText(mDataList.get(position).get("branch_name")+"");
            holder.status.setText(mDataList.get(position).get("level")+"");
            holder.money.setText("会员余额：￥"+mDataList.get(position).get("remain")+"");


            if (mDataList.get(position).containsKey("branch_img")){
                holder.image.setVisibility(View.VISIBLE);
                holder.mdLay.setVisibility(View.GONE);
                Glide.with(mContext).load("http://ly.ahxfk.com/Images/branch_pic/"+mDataList.get(position).get("branch_img")).placeholder(R.mipmap.wbtp_wait).error(R.mipmap.wbtp_error).into(holder.image);
            }else{
                Glide.with(mContext).load("").placeholder(R.mipmap.wbtp_wait).error(R.mipmap.wbtp_error).into(holder.image);
                holder.image.setVisibility(View.GONE);
                holder.mdLay.setVisibility(View.VISIBLE);
                int index = (int) (Math.random() * imgId.length);
                holder.mdLay.setBackgroundResource(imgId[index]);
            }

            if (decimalFormat.format(mDataList.get(position).get("union_type")).equals("0")){
                holder.union_type.setText("本金漫游");
            }else if(decimalFormat.format(mDataList.get(position).get("union_type")).equals("1")){
                holder.union_type.setText("余额漫游");
            }else if(decimalFormat.format(mDataList.get(position).get("union_type")).equals("9")){
                holder.union_type.setText("当前门店没有参与漫游联盟");
            }
        }
        return convertView;
    }

    private static final class ViewHolder {
        TextView branchName, status, money, mdName, union_type;
        ImageView image;
        LinearLayout noDataRootLayout, mdLay;
    }

    private int getScreenWidth() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.widthPixels;
    }

    private int getScreenHeight() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.heightPixels;
    }

}
